package com.wyz.saas.common.utils;

import java.math.BigInteger;

public class ColorUtils {

    public static void main(String[] string) {
        Integer integer = colorStringToInt("#8000ffb3");
        System.out.println(integer);
        System.out.println(colorIntToString(integer));
    }

    public static String colorIntToString(int color) {
        String R, G, B, A;  // 三色字符串
        A = Integer.toHexString(color >> 24 & 0xFF);
        R = Integer.toHexString(color >> 16 & 0xFF);
        G = Integer.toHexString(color >> 8 & 0xFF);
        B = Integer.toHexString(color & 0xFF);
        //判断获取到的R,G,B值的长度 如果长度等于1 给R,G,B值的前边添0
        A = A.length() == 1 ? "0" + A : A;
        R = R.length() == 1 ? "0" + R : R;
        G = G.length() == 1 ? "0" + G : G;
        B = B.length() == 1 ? "0" + B : B;
        return "#" + R + G + B + A;
    }

    public static Integer colorStringToInt(String color) {
        // RGBA
        if (!color.startsWith("#") || color.length() != 9) {
            throw new RuntimeException("color string is not hexadecimal");
        }
        String RGB = color.substring(1, 7);
        String A = color.substring(7);
        System.out.println(RGB + "--" + A);
        BigInteger bigint = new BigInteger(A + RGB, 16);
        return bigint.intValue();
    }
}
